package com.aos.iot.protocol.hll.parser.data;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import com.aos.iot.protocol.hll.util.CodeUtil;
import com.aos.iot.protocol.hll.constant.HllCmd;
import com.aos.iot.protocol.hll.constant.HllProp;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("PipeDataParser")
public class PipeStatusDataParser extends HllDataParser {

    private final List<String> encodeItems = CollUtil.newArrayList(
            HllProp.PIPE_NO.getValue(),
            HllProp.PIPE_STATUS.getValue(),
            HllProp.PIPE_BATTERY_ELECTRIC.getValue(),
            HllProp.PILE_NO.getValue(),
            HllProp.PIPE_OUTPUT_COIL_NO.getValue(),
            HllProp.PIPE_INPUT_COIL_NO.getValue(),
            HllProp.PIPE_INPUT_COIL_VOLTAGE.getValue(),
            HllProp.PIPE_INPUT_COIL_ELECTRIC.getValue(),
            HllProp.PIPE_OUTPUT_COIL_PWM.getValue(),
            HllProp.PIPE_BATTERY_NO.getValue(),
            HllProp.PIPE_OUTPUT_COIL_ELECTRIC.getValue(),
            HllProp.PIPE_OUTPUT_COIL_TEMPERATURE.getValue(),
            HllProp.PIPE_INPUT_COIL_SOFT_VERSION.getValue(),
            HllProp.PIPE_OUTPUT_COIL_SOFT_VERSION.getValue(),
            HllProp.PILE_SOFT_VERSION.getValue(),
            HllProp.PIPE_CURRENT_ELECTRIC.getValue(),
            HllProp.PIPE_INPUT_COIL_HARDWARE_VERSION.getValue(),
            HllProp.PIPE_OUTPUT_COIL_HARDWARE_VERSION.getValue(),
            HllProp.PILE_HARDWARE_VERSION.getValue(),
            HllProp.PIPE_CHARGE_ORDER_NO.getValue()
    );

    private final List<String> decodeItems = CollUtil.newArrayList(
            HllProp.PILE_NO.getValue(),
            HllProp.PIPE_NO.getValue(),
            HllProp.PIPE_STATUS.getValue()
    );

    @Override
    public Dict encode(String payload) {
        return Dict.create()
            .set(HllProp.PIPE_NO.getValue(), sub(payload, 0, 2, false, "00"))
            .set(HllProp.PIPE_STATUS.getValue(), sub(payload, 2, 4, false, "99"))
            .set(HllProp.PIPE_BATTERY_ELECTRIC.getValue(), sub(payload, 4, 6, false, "00"))
            .set(HllProp.PILE_NO.getValue(), sub(payload, 6, 16, true, "0000000000"))
            .set(HllProp.PIPE_OUTPUT_COIL_NO.getValue(), sub(payload, 16, 26, true, "0000000000"))
            .set(HllProp.PIPE_INPUT_COIL_NO.getValue(), sub(payload, 26, 36, true, "0000000000"))
            .set(HllProp.PIPE_INPUT_COIL_VOLTAGE.getValue(), sub(payload, 36, 40, true, "0000"))
            .set(HllProp.PIPE_INPUT_COIL_ELECTRIC.getValue(), sub(payload, 40, 44, true, "0000"))
            .set(HllProp.PIPE_OUTPUT_COIL_PWM.getValue(), sub(payload, 44, 48, true, "0000"))
            .set(HllProp.PIPE_BATTERY_NO.getValue(), sub(payload, 48, 52, true, "0000"))
            .set(HllProp.PIPE_OUTPUT_COIL_ELECTRIC.getValue(), sub(payload, 52, 56, true, "0000"))
            .set(HllProp.PIPE_OUTPUT_COIL_TEMPERATURE.getValue(), sub(payload, 56, 60, true, "0000"))
            .set(HllProp.PIPE_INPUT_COIL_SOFT_VERSION.getValue(), sub(payload, 60, 62, false, "00"))
            .set(HllProp.PIPE_OUTPUT_COIL_SOFT_VERSION.getValue(), sub(payload, 62, 64, false, "00"))
            .set(HllProp.PILE_SOFT_VERSION.getValue(), sub(payload, 64, 66, false, "00"))
            .set(HllProp.PIPE_CURRENT_ELECTRIC.getValue(), sub(payload, 66, 68, false, "00"))
            .set(HllProp.PIPE_INPUT_COIL_HARDWARE_VERSION.getValue(), sub(payload, 68, 70, false, "00"))
            .set(HllProp.PIPE_OUTPUT_COIL_HARDWARE_VERSION.getValue(), sub(payload, 70, 72, false, "00"))
            .set(HllProp.PILE_HARDWARE_VERSION.getValue(), sub(payload, 72, 74, false, "00"))
            .set(HllProp.PIPE_CURRENT_ENERGY.getValue(), sub(payload, 74, 82, true, "00000000"))
            .set(HllProp.PIPE_CHARGE_ORDER_NO.getValue(), sub(payload, 82, 98, true, "0000000000000000"))
            ;
    }

    @Override
    public List<String> encodeItems() {
        return encodeItems;
    }

    @Override
    public String decode(Dict data) {
        return StrUtil.join("",
            CodeUtil.highRotation(data.getStr(HllProp.PILE_NO.getValue())),
            data.getStr(HllProp.PIPE_NO.getValue()),
            data.getStr(HllProp.PIPE_STATUS.getValue())
        );
    }

    @Override
    public List<String> decodeItems() {
        return decodeItems;
    }

    @Override
    public String cmd() {
        return HllCmd.STATUS_UPDATE;
    }

}
